/*
 *  checks.c
 *  test
 *
 *  Created by Phillip LeBlanc on 12/7/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "checks.h"
#include "common.h"

int sniper_last;
int rockets_last;
int camo_last;
int over_last;
int hill_last;
int notif_last;
int notif_time;

void initTimers()
{
	sniper_last = get_sec();
	rockets_last = get_sec();
	camo_last = get_sec();
	over_last = get_sec();
	hill_last = get_sec();
}

int setNotification(int sec)
{
	notif_last = get_sec();
	notif_time = sec;
	
	return 1;
}

int updateSniper(map *themap, int time)
{
	if (themap->sniper <= 0)
		return 0;
	
	int elaptime = get_sec() - sniper_last;
	int i;
	
	for (i = time; i > (time-elaptime); i--)
		switch (i)
		{
			case 30:
				SDL_CreateThread(playSound, sniper30);
				break;
			case 10:
				SDL_CreateThread(playSound, sniper10);
				break;
			case 0:
				SDL_CreateThread(playSound, sniper_aval);
				return themap->sniper;
				break;
		}
	
	sniper_last = get_sec();
	
	return time - elaptime;
}

int updateRockets(map *themap, int time) 
{
	if (themap->rockets <= 0)
		return 0;
	
	int elaptime = get_sec() - rockets_last;
	int i;
	
	for (i = time; i > (time-elaptime); i--)
		switch (i)
		{
			case 30:
				SDL_CreateThread(playSound, rockets30);
				break;
			case 10:
				SDL_CreateThread(playSound, rockets10);
				break;
			case 0:
				SDL_CreateThread(playSound, rockets_aval);
				return themap->rockets;
				break;
		}
	
	rockets_last = get_sec();
	
	return time - elaptime;
}

int updateCamo(map *themap, int time) 
{
	if (themap->camo <= 0)
		return 0;
	
	int elaptime = get_sec() - camo_last;
	int i;
	
	for (i = time; i > (time-elaptime); i--)
		switch (time)
		{
			case 30:
				SDL_CreateThread(playSound, camo30);
				break;
			case 10:
				SDL_CreateThread(playSound, camo10);
				break;
			case 0:
				SDL_CreateThread(playSound, camo_aval);
				return themap->camo;
				break;
		}
	
	camo_last = get_sec();
	
	return time - elaptime;
}

int updateOver(map *themap, int time)
{
	if (themap->over <= 0)
		return 0;
	
	int elaptime = get_sec() - over_last;
	int i;
	
	for (i = time; i > (time-elaptime); i--)
		switch (time)
		{
			case 30:
				SDL_CreateThread(playSound, over30);
				break;
			case 10:
				SDL_CreateThread(playSound, over10);
				break;
			case 0:
				SDL_CreateThread(playSound, over_aval);
				return themap->over;
				break;
		}
	
	over_last = get_sec();
	
	return time - elaptime;
}

int updateHill(map *themap, int time) 
{
	if (themap->gtype.hill.hillmove <= 0)
		return 0;
	
	int elaptime = get_sec() - hill_last;
	int i;
	
	for (i = time; i > (time-elaptime); i--)
		switch (time)
		{
			case 60:
				SDL_CreateThread(playSound, hillonemin);
				break;
			case 30:
				SDL_CreateThread(playSound, hill30sec);
				break;
			case 10:
				SDL_CreateThread(playSound, hilltensec);
				break;
			case 0:
				SDL_CreateThread(playSound, hillmoved);
				return themap->gtype.hill.hillmove;
				break;
		}
	
	hill_last = get_sec();
	
	return time - elaptime;
}

int updateNotification()
{
	int elaptime = get_sec() - notif_last;
	
	notif_time -= elaptime;
	
	if (notif_time <= 0)
		notif_time = 0;
	notif_last = get_sec();
	
	return notif_time;
}

void clearNotification()
{
	notif_time = 0;
}

int check_area(int x, int y)
{
	button coords_main[2];
	
	int i;
	
	coords_main[PAUSE_PLAY].x = 215;
	coords_main[PAUSE_PLAY].y = 360;
	coords_main[PAUSE_PLAY].width = 108;
	coords_main[PAUSE_PLAY].height = 71;
	coords_main[RESET].x = 96;
	coords_main[RESET].y = 360;
	coords_main[RESET].width = 108;
	coords_main[RESET].height = 71;
	
	for (i=0;i<2;i++) {
		if ((x > coords_main[i].x)&&(x< coords_main[i].x + coords_main[i].width)&&(y > coords_main[i].y)&&(y < coords_main[i].y + coords_main[i].height)) {
			return i;
		}  //end if
	}  //end for
	
	return -1;
}

void pause_timer(int *e_time)
{
	setBG(addpath("images/paused.jpg"));
	displayNotification("Paused", 1);
	displayTimers();
	
	int pause_time = *e_time - get_sec();
	
	int done = 0;
	int area;
	SDL_Event event;
	
	while (!done) {
		SDL_PollEvent(&event);
		switch(event.type){
		   case SDL_MOUSEBUTTONDOWN:
			area = check_area(event.button.x, event.button.y);
				if (area == PAUSE_PLAY) {
					*e_time = pause_time + get_sec();
					return;}
			break;
		   case SDL_KEYDOWN:
				if (event.key.keysym.sym == SDLK_SPACE) {
					*e_time = pause_time + get_sec();
					return;}
				if (event.key.keysym.sym == SDLK_ESCAPE) {
					*e_time = pause_time + get_sec();
					return;}
				
			case SDL_QUIT:
                SDL_Quit();
				Mix_CloseAudio();
				TTF_CloseFont(font);
				exit(0);				
		   default:
				break;
		} //end switch
		initTimers();
		SDL_Delay(10);
	} //end while
}
